﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ScriptEditor.Commands;

namespace ScriptEditor
{
    /// <summary>
    /// SelectCommandUI.xaml 的交互逻辑
    /// </summary>
    public partial class SelectCommandUI : Window
    {
        public bool OK { get; private set; } = false;
        public Command CreateCommand { get; private set; } = null;
        public SelectCommandUI()
        {
            InitializeComponent();
            CommandList.Items.Clear();
            foreach (string name in Enum.GetNames(typeof(Command.COMMANDTYPE)))
            {
                if (name.Contains(Fillter.Text))
                {
                    CommandList.Items.Add(name);
                }
            }
            CommandList.SelectedIndex = (CommandList.Items.Count != 0) ? 0 : -1;
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (CommandList.SelectedIndex != -1)
            {
                OK = true;
                //Console.WriteLine(typeof(Command).Namespace + "." + typeof(Command).Name + "_" + CommandList.SelectedItem.ToString());
                CreateCommand = (Command)Activator.CreateInstance(Type.GetType(typeof(Command).Namespace + "." + typeof(Command).Name + "_" + CommandList.SelectedItem.ToString()));
            }
            Close();
        }

        private void Fillter_TextChanged(object sender, TextChangedEventArgs e)
        {
            CommandList.Items.Clear();
            foreach(string name in Enum.GetNames(typeof(Command.COMMANDTYPE)))
            {
                if (name.Contains(Fillter.Text))
                {
                    CommandList.Items.Add(name);
                }
            }
            CommandList.SelectedIndex = (CommandList.Items.Count != 0) ? 0 : -1;
        }
    }
}
